/*   
 * Copyright (c) 2010-2020 DMGIS Ltd. All Rights Reserved.   
 *   
 * This software is the confidential and proprietary information of   
 * DMGIS. You shall not disclose such Confidential Information   
 * and shall use it only in accordance with the terms of the agreements   
 * you entered into with DMGIS.   
 *   
 */
package com.dmgis.qxfw.dataservice.businessservice.common.util;


import com.dmgis.entity.DmMapPoint;
import com.dmgis.qxfw.dataservice.businessservice.common.domain.CEntity;
import com.dmgis.qxfw.dataservice.businessservice.common.domain.MyPoint;
import com.dmgis.qxfw.dataservice.gisservice.service.ITransformService;

import javax.annotation.Resource;
import java.util.*;

/**
 * This class is used 气象数据操作类
 * 
 * @author dsq
 * @version 1.0, 2018年6月28日 下午7:08:23
 */
public class WebCtrlData {
	@Resource
	private static ITransformService transformService;
	/**
	 * 获取经纬度坐标集合
	 *
	 * @param posStr
	 * @return
	 * @author dsq
	 * @date 2018年11月14日
	 */
	public static List<CEntity> GetJWWarningPos(String posStr) {
		List<CEntity> cEntities = new ArrayList<CEntity>();
		try {

			if (!StringUtil.IsNullOrEmpty(posStr)) {
				String[] posArr = posStr.split("\\*");
				for (String pos : posArr) {
					if (pos == null || pos.equals("")) {
						continue;
					}
					CEntity cEntity = new CEntity();
					ArrayList<MyPoint> tempPos = new ArrayList<MyPoint>();
					String strColor = pos.split("#")[0];
					int color = Integer.parseInt(strColor);
					cEntity.setColor(ColorUtil.transWin32ToHtml(color));//
					// 16进制颜色
					// cEntity.setColor(strColor);
					String[] dotset = pos.split("#")[1].split(",");
					for (String strDot : dotset) {
						String[] dot = strDot.split(" ");
						double x = Double.parseDouble(dot[0]);
						double y = Double.parseDouble(dot[1]);
						if (!(Math.abs(x) <= 180 && Math.abs(y) <= 90)) { // 是经纬度坐标则不进行转换
							DmMapPoint ltDot =transformService.LambertConvertToLonLat(x, y);
							x = ltDot.x;
							y = ltDot.y;
						}
						MyPoint dmDot = new MyPoint(x, y);
						tempPos.add(dmDot);
					}
					cEntity.setPosArr(tempPos);
					cEntities.add(cEntity);
				}
				Collections.sort(cEntities, new SortByColor());
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return cEntities;
	}

	private Map<String, String> GetNameAndPath() {
		Map<String, String> NameAndPath = new HashMap<String, String>();
		NameAndPath.put("晴", "00");
		NameAndPath.put("多云", "01");
		NameAndPath.put("阴", "02");
		NameAndPath.put("阵雨", "03");
		NameAndPath.put("雷阵雨", "04");
		NameAndPath.put("雷阵雨伴冰雹", "05");
		NameAndPath.put("小雨", "07");
		NameAndPath.put("小阵雨", "07");// 南充修改
		NameAndPath.put("小到中雨", "21");
		NameAndPath.put("中雨", "08");
		NameAndPath.put("中到大雨", "22");
		NameAndPath.put("大雨", "09");
		NameAndPath.put("大到暴雨", "23");
		NameAndPath.put("暴雨", "10");
		NameAndPath.put("暴雨到大暴雨", "24");
		NameAndPath.put("大暴雨", "11");
		NameAndPath.put("大暴雨到特大暴雨", "25");
		NameAndPath.put("特大暴雨", "12");
		NameAndPath.put("雨夹雪", "06");
		NameAndPath.put("阵雪", "13");
		NameAndPath.put("小雪", "14");
		NameAndPath.put("小到中雪", "26");
		NameAndPath.put("中雪", "15");
		NameAndPath.put("中到大雪", "27");
		NameAndPath.put("大雪", "16");
		NameAndPath.put("大到暴雪", "28");
		NameAndPath.put("暴雪", "17");
		NameAndPath.put("雾", "18");
		NameAndPath.put("冻雨", "19");
		NameAndPath.put("沙尘暴", "20");
		NameAndPath.put("浮尘", "29");
		NameAndPath.put("扬沙", "30");
		NameAndPath.put("强沙尘暴", "31");
		NameAndPath.put("微量小雨", "32");
		return NameAndPath;
	}

	/**
	 * 根据天气气象中文转换成图片名
	 * 
	 * @param strtq
	 * @return
	 * @author dsq
	 * @date 2018年6月28日
	 */
	public static String WeatherNameToPicPath(String strtq) {
		WebCtrlData wcd = new WebCtrlData();
		if (strtq.contains("阴")) {
			strtq = "阴";
		}
		if (strtq.contains("多云")) {
			strtq = "多云";
		}
		Map<String, String> NameAndPath = wcd.GetNameAndPath();
		if (NameAndPath.containsKey(strtq)) {
			return NameAndPath.get(strtq);
		}
		return strtq;
	}

	/**
	 * 根据天气编码获取天气名称
	 * 
	 * @param strtq
	 * @return
	 * @author dsq
	 * @date 2018年6月28日
	 */
	public String GetWeatherTq(String strtq) {
		try {
			switch (strtq) {
			case "00":
				strtq = "晴";
				break;
			case "01":
				strtq = "多云";
				break;
			case "02":
				strtq = "阴";
				break;
			case "03":
				strtq = "阵雨";
				break;
			case "04":
				strtq = "雷阵雨";
				break;
			case "05":
				strtq = "雷阵雨伴冰雹";
				break;
			case "07":
				strtq = "小雨";
				break;
			case "21":
				strtq = "小到中雨";
				break;
			case "08":
				strtq = "中雨";
				break;
			case "22":
				strtq = "中到大雨";
				break;
			case "09":
				strtq = "大雨";
				break;
			case "23":
				strtq = "大到暴雨";
				break;
			case "10":
				strtq = "暴雨";
				break;
			case "24":
				strtq = "暴雨到大暴雨";
				break;
			case "11":
				strtq = "大暴雨";
				break;
			case "25":
				strtq = "大暴雨到特大暴雨";
				break;
			case "12":
				strtq = "特大暴雨";
				break;
			case "06":
				strtq = "雨夹雪";
				break;
			case "13":
				strtq = "阵雪";
				break;
			case "14":
				strtq = "小雪";
				break;
			case "26":
				strtq = "小到中雪";
				break;
			case "15":
				strtq = "中雪";
				break;
			case "27":
				strtq = "中到大雪";
				break;
			case "16":
				strtq = "大雪";
				break;
			case "28":
				strtq = "大到暴雪";
				break;
			case "17":
				strtq = "暴雪";
				break;
			case "18":
				strtq = "雾";
				break;
			case "19":
				strtq = "冻雨";
				break;
			case "20":
				strtq = "沙尘暴";
				break;
			case "29":
				strtq = "浮尘";
				break;
			case "30":
				strtq = "扬沙";
				break;
			case "31":
				strtq = "强沙尘暴";
				break;
			case "32":
				strtq = "微量小雨";
				break;
			default:
				break;
			}
			return strtq;
		} catch (Exception ex) {
			return "0";
		}
	}

	/**
	 * 得到风向信息
	 * 
	 * @param strFx
	 * @return
	 * @author dsq
	 * @date 2018年6月28日
	 */
	public String GetWeatherFx(String strFx) {
		try {
			switch (strFx) {
			case "0":
				strFx = "微风";
				break;
			case "4":
				strFx = "南风";
				break;
			case "8":
				strFx = "北风";
				break;
			case "5":
				strFx = "西南风";
				break;
			case "7":
				strFx = "西北风";
				break;
			case "3":
				strFx = "东南风";
				break;
			case "1":
				strFx = "东北风";
				break;
			case "2":
				strFx = "东风";
				break;
			case "6":
				strFx = "西风";
				break;
			case "9":
				strFx = "旋转/不定";
				break;

			default:
				break;
			}
			return strFx;
		} catch (Exception e) {
			return "0";
		}
	}

	/**
	 * 根据风速获取风级
	 * 
	 * @param fengsu
	 * @return
	 * @author dsq
	 * @date 2018年6月28日
	 */
	public static int FengSuToFengJi(double fengsu) {
		// if (fengsu > 0 && fengsu < 0.3) {
		// 	return 0;
		// } else if (fengsu > 0.2 && fengsu < 1.6) {
		// 	return 1;
		// } else if (fengsu > 1.5 && fengsu < 3.4) {
		// 	return 2;
		// } else if (fengsu > 3.3 && fengsu < 5.5) {
		// 	return 3;
		// } else if (fengsu > 5.4 && fengsu < 8) {
		// 	return 4;
		// } else if (fengsu > 7.9 && fengsu < 10.8) {
		// 	return 5;
		// } else if (fengsu > 10.7 && fengsu < 13.9) {
		// 	return 6;
		// } else if (fengsu > 13.8 && fengsu < 17.2) {
		// 	return 7;
		// } else if (fengsu > 17.1 && fengsu < 20.8) {
		// 	return 8;
		// } else if (fengsu > 20.7 && fengsu < 24.5) {
		// 	return 9;
		// } else if (fengsu > 24.4 && fengsu < 28.5) {
		// 	return 10;
		// } else if (fengsu > 28.4 && fengsu < 32.7) {
		// 	return 11;
		// } else if (fengsu > 36.9 && fengsu < 41.5) {
		// 	return 12;
		// }else if(fengsu < 0){
		// 	return 100;
		// }
		// return 0;
		if (fengsu >= 0.0 && fengsu < 0.3) {
			return 0;
		} else if (fengsu >= 0.3 && fengsu < 1.6) {
			return 1;
		} else if (fengsu >= 1.6 && fengsu < 3.4) {
			return 2;
		} else if (fengsu >= 3.4 && fengsu < 5.5) {
			return 3;
		} else if (fengsu >= 5.5 && fengsu < 8) {
			return 4;
		} else if (fengsu >= 8 && fengsu < 10.8) {
			return 5;
		} else if (fengsu >= 10.8 && fengsu < 13.9) {
			return 6;
		} else if (fengsu >= 13.9 && fengsu < 17.2) {
			return 7;
		} else if (fengsu >= 17.2 && fengsu < 20.8) {
			return 8;
		} else if (fengsu >= 20.8 && fengsu < 24.5) {
			return 9;
		} else if (fengsu >= 24.5 && fengsu < 28.5) {
			return 10;
		} else if (fengsu >= 28.5 && fengsu < 36.9) {
			return 11;
		} else if (fengsu >= 36.9 && fengsu < 41.5) {
			return 12;
		}else if(fengsu < 0){
			return 100;
		}
		return 0;

	}

	/**
	 * 获取风级名称
	 * 
	 * @param strFs
	 * @return
	 * @author dsq
	 * @date 2018年6月28日
	 */
	public String GetWeatherFs(String strFs) {
		if (strFs.contains("级"))
			return strFs;
		if (strFs.trim() == "") {
			strFs = "0";
		} else {
			strFs = strFs.split("\\.")[0] == null ? strFs : strFs.split("\\.")[0];// 注意，在Java中split参数为string类型，并且特殊字符前面要加上\\作为转义符，如（'.','*','|','\'）'\'要用\\\\分隔
		}
		try {
			switch (strFs) {
			case "0":
				strFs = "≤3级";
				break;
			case "1":
				strFs = "3-4级";
				break;
			case "2":
				strFs = "4-5级";
				break;
			case "3":
				strFs = "5-6级";
				break;
			case "4":
				strFs = "6-7级";
				break;
			case "5":
				strFs = "7-8级";
				break;
			case "6":
				strFs = "8-9级";
				break;
			case "7":
				strFs = "9-10级";
				break;
			case "8":
				strFs = "10-11级";
				break;
			case "9":
				strFs = "11-12级";
				break;
			default:
				break;
			}
			return strFs;
		} catch (Exception ex) {
			return "0";
		}
	}

	/**
	 * 根据角度获取风向信息
	 * 
	 * @param strFx
	 * @return
	 * @author dsq
	 * @date 2018年6月28日
	 */
	public static String GetAllFx(String strFx) {
		try {
			if (strFx != "") {
				double j = Double.parseDouble(strFx);
				if (j > 202.5 && j < 247.5) {
					strFx = "西南风";

				} else if (j > 292.5 && j < 360) {
					strFx = "西北风";

				} else if (j > 112.5 && j < 157.5) {
					strFx = "东南风";

				} else if (j > 22.5 && j < 67.5) {
					strFx = "东北风";

				}
				// strQFx = "旋转/不定";
				else if (j > 157.5 && j < 202.5) {
					strFx = "南风";

				} else if (j > 0 && j < 22.5) {
					strFx = "北风";
				} else if (j > 67.5 && j < 112.5) {
					strFx = "东风";

				} else if (j > 247.5 && j < 292.5) {
					strFx = "西风";
				} else {
					strFx = "东风";
				}

			}
			return strFx;
		} catch (Exception err) {
			return "东风";
		}
	}

	public static double Getddformddffmm(Double dds) {
		try {
			if (dds > 180) {
				int dd = (int) (dds / 10000);
				int ff = (int) ((dds - dd * 10000) / 100);
				int mm = (int) (dds - dd * 10000 - ff * 100);
				dds = dd + ff / 60.00 + mm / 3600.00;
				return dds;
			} else {
				return dds;
			}
		} catch (Exception ex) {
			return 0.0;
		}
	}

	/**
	 * 获取经纬度坐标集合
	 * 
	 * @param
	 * @return
	 * @author dsq
	 * @date 2018年11月14日
	 */
//	public static List<CEntity> GetJWWarningPos(String posStr) {
//		List<CEntity> cEntities = new ArrayList<CEntity>();
//		try {
//
//			DmCloud dmCloud = new DmCloud();
//			if (!StringUtil.IsNullOrEmpty(posStr)) {
//				String[] posArr = posStr.split("\\*");
//				for (String pos : posArr) {
//					if (pos == null || pos.equals("")) {
//						continue;
//					}
//					CEntity cEntity = new CEntity();
//					ArrayList<MyPoint> tempPos = new ArrayList<MyPoint>();
//					String strColor = pos.split("#")[0];
//					int color = Integer.parseInt(strColor);
//					cEntity.setColor(ColorUtil.transWin32ToHtml(color));//
//					// 16进制颜色
//					// cEntity.setColor(strColor);
//					String[] dotset = pos.split("#")[1].split(",");
//					for (String strDot : dotset) {
//						String[] dot = strDot.split(" ");
//						double x = Double.parseDouble(dot[0]);
//						double y = Double.parseDouble(dot[1]);
//						if (!(Math.abs(x) <= 180 && Math.abs(y) <= 90)) { // 是经纬度坐标则不进行转换
//							DmDot ltDot = dmCloud.LambertConvertToLonLat(x, y);
//							x = ltDot.x;
//							y = ltDot.y;
//						}
//						MyPoint dmDot = new MyPoint(x, y);
//						tempPos.add(dmDot);
//					}
//					cEntity.setPosArr(tempPos);
//					cEntities.add(cEntity);
//				}
//				Collections.sort(cEntities, new SortByColor());
//			}
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		return cEntities;
//	}
	public  static Double ToDegress(String d,String f,String m){
		double v = Double.parseDouble(d) + Double.parseDouble(f) / 60 + Double.parseDouble(m) / 3600;
		return v;
	}

}

/**
 * This class is used for 面实体排序
 * 
 * @author dsq
 * @version 1.0, 2018年7月14日 下午4:42:35
 */
//class SortByColor implements Comparator<Object> {
//	public int compare(Object o1, Object o2) {
//		CEntity entity1 = (CEntity) o1;
//		CEntity entity2 = (CEntity) o2;
//		if (entity1.getColor().compareTo(entity2.getColor())>0)
//			return 1;
//		return -1;
//	}
//}
